import pymysql

'''
插入操作
'''

# Open database connection
db = pymysql.connect('localhost', 'root', 'root', 'py_test')

# prepare a cursor object using cursor() method
cursor = db.cursor()

# prepare SQL query to insert a record into the database
sql = '''INSERT INTO EMPLOYEE(FIRST_NAME,
   LAST_NAME, AGE, SEX, INCOME)
   VALUES ('Mac', 'Su', 20, 'M', 5000)'''

try:
    # Execute the SQL command
    cursor.execute(sql)
    # Commit your changes in the database
    db.commit()
except:
    # Rollback in case there is any error
    db.rollback()
print(sql)
print('Yes, Insert Successful.')

# 再次插入一条记录
sql = '''INSERT INTO EMPLOYEE(FIRST_NAME,
   LAST_NAME, AGE, SEX, INCOME)
   VALUES ('Kobe', 'Bryant', 40, 'M', 8000)'''

try:
    # Execute the SQL command
    cursor.execute(sql)
    # Commit your changes in the database
    db.commit()
except:
    # Rollback in case there is any error
    db.rollback()
print(sql)
print('Yes, Insert Successful.')

# disconnect from server
db.close()